<template>
	<view>
		<view class="px-20 pt-20 " :style="`padding-bottom:${height}px;`">
			<view class="flex flex-center   flex-column">
				<image class="w-80 h-80 rounded-circle" :src="WEBCONFIG?.web_logo"></image>
				<text class="fw-600 fs-20 mt-10">{{WEBCONFIG?.web_name}}</text>
			</view>
			<view class="bg-blue round-30 text-white fs-16 text-center py-10 mt-30 vw-60" :data-eventsync="true"
				@tap="login">
				一键登录
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, defineEmits } from 'vue'
	import system from '@/utils/system'
	import { useRefs, useWebConfigStore, useUserStore } from '@/stores'
	import { useStorage, $http } from '@/utils'
	// #ifdef H5
	const height=ref(100)
	// #endif
	// #ifndef H5
	const height=ref(20)
	// #endif
	const { WEBCONFIG } = useRefs(useWebConfigStore())
	const emit = defineEmits(['close'])

	const login = async () => {
		let appid = system.appid()
		let pid = useStorage().get('PID') || 0
		uni.showLoading({
			title: '登录中...'
		})
		const {  setUserInfo } = useUserStore()
		let provider = 'weixin'
		let data = {
			source: provider,
			pid: pid
		}
		// #ifdef MP-TOUTIAO
		provider = 'toutiao'
		data.source = 'douyin'
		await (new Promise((res, err) => {
			tt.getUserProfile({
				force: true,
				success(result) {
					data.nickname = result.userInfo.nickName
					data.avatar = result.userInfo.avatarUrl
					res(true)
					
				},
				fail(e) {
					uni.hideToast()
					err(e)
				}
			})
		}))
		// #endif

		// #ifndef H5
		uni.login({
			provider: provider,
			success(res) {
				data.code = res.code
				$http.post('User/login', data).then(res => {
					setUserInfo(res.data)
					emit('close')
				})
			}
		})
		// #endif
		
		// #ifdef H5
		$http.post('User/offiaccountLogin',{pid:pid}).then(res=>{
			location.assign(res.data);
		})
		// #endif
	}
</script>

<style>
	.bg-blue{
		background-color: #3D5CFF;
	}
</style>